/**
 * Created by root on 2017/8/11.
 *返回文件夹内容
 */
window.onload=function(){
    var mytable=document.getElementById("mynote_table");

    var request=new XMLHttpRequest();
    request.open("POST","i/note/note/noteList", true);
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    var message="type=1";
    request.onreadystatechange=function(){
        if(request.readyState==4)
        {
            if(request.status==200)
            {
                var data=JSON.parse(request.responseText);
                for(var i=0;i<data.length;i++)
                {
                    var myrow=mytable.insertRow(1);
                    var w=myrow.insertCell(0); //标题
                    var x=myrow.insertCell(1); //日期
                    var y=myrow.insertCell(2); //noteId
                    var z=myrow.insertCell(3); //删除按钮
                    w.setAttribute("class","wrap");
                    w.innerHTML=data[i].noteTitle;
                    x.innerHTML=data[i].noteTimeUpdate;
                    var y_id=document.createElement("div");
                    y_id.innerHTML=data[i].noteId;
                    y.appendChild(y_id);
                    y_id.setAttribute("style","display:block");      //隐藏
                    var newbutton=document.createElement("input");
                    newbutton.setAttribute("type","button");
                    newbutton.setAttribute("value","删除");
                    z.appendChild(newbutton);
                }
            }
            else
            {
                alert("请求错误"+request.status);
            }
        }
    }
    request.send(message);

    function setCookie(cname,cvalue)
    {
        document.cookie = cname + "=" + cvalue + "; ";
    }

    var num=mytable.rows.length;
    for(var i=1;i<num;i++)          //查看笔记
    {
        //单条笔记
        mytable.rows[i].cells[0].onclick=function(){
            var id=this.parentNode.cells[2].firstChild.innerHTML;
            setCookie("note_show",id);
            window.location.href="show.html"
        }
        //删除按钮
        mytable.rows[i].cells[3].firstChild.onclick=function(){
            this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
            var id=this.parentNode.parentNode.cells[2].firstChild.innerHTML;

            var xmlhttp=XMLHttpRequest();
            xmlhttp.open("POST","i/note/note/update_type",true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            var send_message="noteId="+id+"type=2";
            xmlhttp.onreadystatechange=function(){
                if(xmlhttp.readyState==4)
                {
                    if(xmlhttp.status==200)
                    {
                        var data=JSON.parse(xmlhttp.responseText);
                        if(data.result==1)
                        {
                            alert("删除成功");
                        }
                    }
                    else{
                        alert("删除失败"+xmlhttp.status);
                    }
                }
            }
            xmlhttp.send(send_message);
        }

    }
}